package com.sthz.facepay.mapper;

import com.sthz.facepay.model.Advertisement;
import com.sthz.facepay.model.Card;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface CardMapper {
    @Delete({
        "delete from st_card",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into st_card (id, itid, ",
        "title, logo, colour, ",
        "name, value, grant_number, ",
        "user_limitation, explain, ",
        "min_consume_money, effective_start_time, ",
        "effective_end_time, activity_effective_start_time, ",
        "activity_effective_end_time)",
        "values (#{id,jdbcType=INTEGER}, #{itid,jdbcType=INTEGER}, ",
        "#{title,jdbcType=VARCHAR}, #{logo,jdbcType=VARCHAR}, #{colour,jdbcType=VARCHAR}, ",
        "#{name,jdbcType=VARCHAR}, #{value,jdbcType=VARCHAR}, #{grant_number,jdbcType=INTEGER}, ",
        "#{user_limitation,jdbcType=INTEGER}, #{explain,jdbcType=VARCHAR}, ",
        "#{min_consume_money,jdbcType=DOUBLE}, #{effective_start_time,jdbcType=TIMESTAMP}, ",
        "#{effective_end_time,jdbcType=TIMESTAMP}, #{activity_effective_start_time,jdbcType=TIMESTAMP}, ",
        "#{activity_effective_end_time,jdbcType=TIMESTAMP})"
    })
    int insert(Card record);

    @InsertProvider(type=CardSqlProvider.class, method="insertSelective")
    int insertSelective(Card record);

    @Select({
        "select",
        "id, itid, title, logo, colour, name, value, grant_number, user_limitation, explain, ",
        "min_consume_money, effective_start_time, effective_end_time, activity_effective_start_time, ",
        "activity_effective_end_time",
        "from st_card",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="itid", property="itid", jdbcType=JdbcType.INTEGER),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="logo", property="logo", jdbcType=JdbcType.VARCHAR),
        @Result(column="colour", property="colour", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="value", property="value", jdbcType=JdbcType.VARCHAR),
        @Result(column="grant_number", property="grant_number", jdbcType=JdbcType.INTEGER),
        @Result(column="user_limitation", property="user_limitation", jdbcType=JdbcType.INTEGER),
        @Result(column="explain", property="explain", jdbcType=JdbcType.VARCHAR),
        @Result(column="min_consume_money", property="min_consume_money", jdbcType=JdbcType.DOUBLE),
        @Result(column="effective_start_time", property="effective_start_time", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="effective_end_time", property="effective_end_time", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="activity_effective_start_time", property="activity_effective_start_time", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="activity_effective_end_time", property="activity_effective_end_time", jdbcType=JdbcType.TIMESTAMP)
    })
    Card selectByPrimaryKey(Integer id);

    @UpdateProvider(type=CardSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Card record);

    @Update({
        "update st_card",
        "set itid = #{itid,jdbcType=INTEGER},",
          "title = #{title,jdbcType=VARCHAR},",
          "logo = #{logo,jdbcType=VARCHAR},",
          "colour = #{colour,jdbcType=VARCHAR},",
          "name = #{name,jdbcType=VARCHAR},",
          "value = #{value,jdbcType=VARCHAR},",
          "grant_number = #{grant_number,jdbcType=INTEGER},",
          "user_limitation = #{user_limitation,jdbcType=INTEGER},",
          "explain = #{explain,jdbcType=VARCHAR},",
          "min_consume_money = #{min_consume_money,jdbcType=DOUBLE},",
          "effective_start_time = #{effective_start_time,jdbcType=TIMESTAMP},",
          "effective_end_time = #{effective_end_time,jdbcType=TIMESTAMP},",
          "activity_effective_start_time = #{activity_effective_start_time,jdbcType=TIMESTAMP},",
          "activity_effective_end_time = #{activity_effective_end_time,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Card record);

    @Select({
            "select * from st_card a inner join st_interval t on a.itid = t.id"
    })

    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="itid", property="itid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="logo", property="logo", jdbcType=JdbcType.VARCHAR),
            @Result(column="colour", property="colour", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="value", property="value", jdbcType=JdbcType.VARCHAR),
            @Result(column="grant_number", property="grant_number", jdbcType=JdbcType.INTEGER),
            @Result(column="user_limitation", property="user_limitation", jdbcType=JdbcType.INTEGER),
            @Result(column="explain", property="explain", jdbcType=JdbcType.VARCHAR),
            @Result(column="min_consume_money", property="min_consume_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="effective_start_time", property="effective_start_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="effective_end_time", property="effective_end_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="activity_effective_start_time", property="activity_effective_start_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="activity_effective_end_time", property="activity_effective_end_time", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Card> findAll();

    /**
     * 根据ID查询数据
     * @return
     */
    @Select({
            "select * from st_card a left join st_interval t on a.itid = t.id where a.id=#{id}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="itid", property="itid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="logo", property="logo", jdbcType=JdbcType.VARCHAR),
            @Result(column="colour", property="colour", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="value", property="value", jdbcType=JdbcType.VARCHAR),
            @Result(column="grant_number", property="grant_number", jdbcType=JdbcType.INTEGER),
            @Result(column="user_limitation", property="user_limitation", jdbcType=JdbcType.INTEGER),
            @Result(column="explain", property="explain", jdbcType=JdbcType.VARCHAR),
            @Result(column="min_consume_money", property="min_consume_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="effective_start_time", property="effective_start_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="effective_end_time", property="effective_end_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="activity_effective_start_time", property="activity_effective_start_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="activity_effective_end_time", property="activity_effective_end_time", jdbcType=JdbcType.TIMESTAMP)
    })

    Card findById_card(int id);

    /**
     * title模糊查询
     * @return
     */
    @Select({
            "select * from st_card a inner join st_interval t on a.itid = t.id where  title like '%'"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="itid", property="itid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="logo", property="logo", jdbcType=JdbcType.VARCHAR),
            @Result(column="colour", property="colour", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="value", property="value", jdbcType=JdbcType.VARCHAR),
            @Result(column="grant_number", property="grant_number", jdbcType=JdbcType.INTEGER),
            @Result(column="user_limitation", property="user_limitation", jdbcType=JdbcType.INTEGER),
            @Result(column="explain", property="explain", jdbcType=JdbcType.VARCHAR),
            @Result(column="min_consume_money", property="min_consume_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="effective_start_time", property="effective_start_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="effective_end_time", property="effective_end_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="activity_effective_start_time", property="activity_effective_start_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="activity_effective_end_time", property="activity_effective_end_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="itid", property="interval", one=@One(select="com.sthz.facepay.mapper.IntervalMapper.getById",fetchType = FetchType.EAGER))

    })
    List<Card> findBylike_card();

}